<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="产品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM编码" prop="bomCode">
        <el-input
          v-model="queryParams.bomCode"
          placeholder="请输入BOM编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM版本" prop="bomName">
        <el-input
          v-model="queryParams.bomName"
          placeholder="请输入BOM版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="excreteList" border>
      <el-table-column label="拆解编码" align="center" prop="code">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['md:excrete:type']"
          >{{scope.row.code}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="产品物料编码" align="center" prop="itemCode" width="140" />
      <el-table-column label="产品物料名称" align="center" prop="itemName" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" :show-overflow-tooltip="true"/>
      <el-table-column label="BOM编码" align="center" prop="bomCode" width="160" />
      <el-table-column label="BOM版本" align="center" prop="bomName" :show-overflow-tooltip="true"/>
      <el-table-column label="外协计划编码" align="center" prop="outsourcingPlanCode" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status == 3">未添加</el-tag>
          <el-tag type="info" v-if="scope.row.status == 0">草稿</el-tag>
          <el-tag type="success" v-if="scope.row.status == 1">已审批</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['md:excrete:type']">
              查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-plus"
              @click="addOutsourcePlan(scope.row)"
              v-if="!(scope.row.outsourcingPlanCode)"
              v-hasPermi="['pro:outsourcingplan:add']">
              添加外协计划</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编码" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请选择产品编码" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编号" prop="code">
              <el-input v-model="form.code" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" type="textarea" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="BOM版本" prop="bomName">
              <el-input v-model="form.bomName" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="BOM编码" prop="bomCode">
              <el-input v-model="form.bomCode" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" readonly />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider content-position="center">外协拆分物料</el-divider>

      <el-table :data="excreteLines">
        <el-table-column label="编码" align="center" prop="itemCode" width="140"/>
        <el-table-column label="名称" align="center" prop="itemName" :show-overflow-tooltip="true"/>
        <el-table-column label="类型" align="center" prop="itemType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mes_item_product" :value="scope.row.itemType"/>
          </template>
        </el-table-column>
        <el-table-column label="单位" align="center" prop="unitOfMeasure" :show-overflow-tooltip="true"/>
        <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
        <el-table-column label="剩余库存" align="center" prop="quantityOnhand" />
        <el-table-column label="使用量" align="center" prop="used"/>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExcreteByType, getType } from "@/api/mes/md/excrete";

export default {
  name: "OutsourceExcrete",
  dicts: [ 'bom_excrete_type', 'mes_item_product' ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品拆分表格数据
      excreteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: null,
        itemName: null,
        bomCode: null,
        bomName: null,
        status: '确认',
        excreteType: 4
      },
      // 表单参数
      form: {},
      excreteLines: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品拆分列表 */
    getList() {
      this.loading = true;
      listExcreteByType(this.queryParams).then(response => {
        this.excreteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        bomId: null,
        bomCode: null,
        bomName: null,
        used: null,
        excreteType: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.excreteLines = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleView(row) {
      const id = row.id;
      getType(id, '4').then(response => {
        this.form = response.data;
        this.excreteLines = response.data.excreteItems;
        this.open = true;
        this.title = "查看外协拆分物料";
      });
    },
    addOutsourcePlan(row){
      this.$router.push({name: 'OutsourcingPlan', params: row});
    },
  }
};
</script>
